import { _decorator, Component, Node, size, Size, UIOpacity, UITransform } from 'cc';
import { RenderMananger2D } from './RenderMananger2D';
const { ccclass, property, requireComponent } = _decorator;


export enum RenderItemRenderState {
    /**
     * 不透明
     */
    OPACITY,

    /**
     * 透明
     */
    TRANSPARENT
}

/**
 * 
 */
@ccclass('RenderItem')
@requireComponent(UIOpacity)
export class RenderItem extends Component {
    private _uiOpacity: UIOpacity

    protected start() {
        this._uiOpacity = this.node.getComponent(UIOpacity)
        RenderMananger2D.add(this)
    }

    protected onDisable(): void {
        RenderMananger2D.remove(this)
    }

    protected onDestroy(): void {
        RenderMananger2D.remove(this)
    }

    private _state: RenderItemRenderState = RenderItemRenderState.OPACITY
    public setState(state_rirs_: RenderItemRenderState) {
        if (this._state == state_rirs_)
            return

        this._state = state_rirs_
        if (this._state == RenderItemRenderState.OPACITY) {
            this._uiOpacity.opacity = 255
        } else {
            this._uiOpacity.opacity = 0
        }
    }

    public getVisibleHalfWH(): Size {
        const ut_com = this.node.getComponent(UITransform)
        return size(ut_com.width / 2, ut_com.height / 2)
    }
}